package com.jic.manager.controller.manage.conf;

import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.manager.entity.SysCarouselFigure;
import com.jic.conf.request.SysCarouselFigureRequest;
import com.jic.conf.response.SysCarouselFigureResponse;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.service.SysCarouselFigureService;
import com.jic.manager.utils.BeanUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: MJ
 * @date: 2020-01-09
 * @remark: 轮播图后台管理系统的服务
 */
@Slf4j
@RestController
@RequestMapping("/sysCarouselFigure")
public class SysCarouselFigureController{

    @Autowired
    SysCarouselFigureService sysCarouselFigureService;
    @Resource
    private RedisClientTemplate redisClientTemplate;

    /*
    * 分页查询
    * */
    @ApiOperation(value = "获取所有轮播图")
    @ResponseBody
    @RequestMapping("/queryList")
    public RestResult<PageResult<SysCarouselFigureResponse>> queryList(@RequestBody SysCarouselFigureRequest form) {
        PageResult<SysCarouselFigure> result = sysCarouselFigureService.queryPage(form);
        List<SysCarouselFigureResponse> sysCarouselFigureResponseList = result.getRows().stream().map(temp -> {
            SysCarouselFigureResponse sysCarouselFigureResponse = BeanUtil.copy(temp, SysCarouselFigureResponse.class);
            return sysCarouselFigureResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<SysCarouselFigureResponse> pageResult = new PageResult<>();
        pageResult.setRows(sysCarouselFigureResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }



    /*
    * 新增/修改轮播图
    * */
    @ApiOperation(value = "新增/修改轮播图")
    @ResponseBody
    @RequestMapping("/editSysCarouselFigure")
    public RestResult<String> editSysCarouselFigure(@RequestBody SysCarouselFigureRequest form) {
        RestResult restResult = new RestResult<>();
        try {
            //修改
            if(null!=form.getId()&&!"".equals (form.getId())){
                form.setUpdater(null==form.getAdminOperate()?0:form.getAdminOperate().getOperator());
                form.setUpdaterName(null==form.getAdminOperate()?"":form.getAdminOperate().getOperateName());
                sysCarouselFigureService.update(form);
                restResult=RestResult.success(restResult);
            }else {
                form.setCreator(null==form.getAdminOperate()?0:form.getAdminOperate().getOperator());
                form.setCreatorName(null==form.getAdminOperate()?"":form.getAdminOperate().getOperateName());
                sysCarouselFigureService.insert(form);
                restResult=RestResult.success(restResult);
            }

        } catch (Exception e) {
            log.error("新增/修改轮播图异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;
    }


    /*
     * 删除轮播图
     * */
    @ApiOperation(value = "删除轮播图")
    @ResponseBody
    @RequestMapping("/deleteInfo")
    public RestResult<String> deleteInfo(@RequestBody SysCarouselFigureRequest form) {
        RestResult restResult = new RestResult<>();
        try {
            String[] checked = form.getIds().split(",");
            for(int i=0;i<checked.length;i++){
                sysCarouselFigureService.delete(Long.valueOf(checked[i]));
            }
        } catch (Exception e) {
            log.error("删除轮播图异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return RestResult.success("成功");
    }
}
